<template>
  <div class="phone-input-overlay" v-if="visible">
    <div class="phone-input-dialog">
      <!-- 返回按钮 -->
      <div class="dialog-header">
        <div class="back-button" @click="handleCancel">
          <el-icon><ArrowLeft /></el-icon>
        </div>
      </div>

      <!-- 主要内容 -->
      <div class="dialog-body">
        <div class="dialog-title">请输入新手机号</div>
        <div class="dialog-description">
          换绑新手机号之后,可以用新的手机号及当前密码登录
        </div>

        <!-- 手机号输入框 -->
        <div class="phone-input-wrapper">
          <div class="country-code-selector">
            <span class="country-code">+86</span>
            <el-icon class="arrow-down"><ArrowDown /></el-icon>
          </div>
          <div class="input-divider"></div>
          <input
            v-model="phoneNumber"
            type="tel"
            class="phone-input"
            placeholder="请输入手机号"
            maxlength="11"
            @input="handleInput"
            @blur="validatePhone"
          />
          <div v-if="phoneError" class="error-message">{{ phoneError }}</div>
        </div>
      </div>

      <!-- 确认按钮 -->
      <div class="dialog-footer">
        <button
          class="confirm-button"
          :class="{ 'disabled': !isValid }"
          :disabled="!isValid"
          @click="handleConfirm"
        >
          确认
        </button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, watch } from 'vue'
import { ArrowLeft, ArrowDown } from '@element-plus/icons-vue'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

const phoneNumber = ref('')
const phoneError = ref('')

// 验证手机号格式
const isValid = computed(() => {
  const phone = phoneNumber.value.trim()
  const phoneRegex = /^1[3-9]\d{9}$/
  return phoneRegex.test(phone)
})

// 监听visible变化，重置输入
watch(() => props.visible, (newVal) => {
  if (newVal) {
    phoneNumber.value = ''
    phoneError.value = ''
  }
})

const handleInput = (e) => {
  // 只允许输入数字
  phoneNumber.value = e.target.value.replace(/\D/g, '')
  // 清除错误信息
  phoneError.value = ''
}

const validatePhone = () => {
  phoneError.value = ''
  const phone = phoneNumber.value.trim()
  
  // 只在有内容时验证格式
  if (phone && !/^1[3-9]\d{9}$/.test(phone)) {
    phoneError.value = '请输入正确的手机号'
  }
}

const handleConfirm = () => {
  if (isValid.value) {
    emit('update:visible', false)
    emit('confirm', phoneNumber.value.trim())
  }
}

const handleCancel = () => {
  emit('update:visible', false)
  emit('cancel')
}
</script>

<style lang="scss" scoped>
.phone-input-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ffffff;
  z-index: 2000;
  
  .phone-input-dialog {
    width: 100%;
    height: 100%;
    background: #ffffff;
    display: flex;
    flex-direction: column;
    
    .dialog-header {
      padding: 16px 20px;
      border-bottom: 1px solid #f0f0f0;
      
      .back-button {
        width: 32px;
        height: 32px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        color: #000000;
        
        .el-icon {
          font-size: 20px;
        }
        
        &:active {
          opacity: 0.6;
        }
      }
    }
    
    .dialog-body {
      flex: 1;
      padding: 32px 20px 24px 20px;
      overflow-y: auto;
      
      .dialog-title {
        font-size: 22px;
        font-weight: bold;
        color: #000000;
        text-align: center;
        margin-bottom: 12px;
      }
      
      .dialog-description {
        font-size: 14px;
        color: #909399;
        text-align: center;
        margin-bottom: 32px;
        line-height: 1.5;
      }
      
      .phone-input-wrapper {
        display: flex;
        align-items: center;
        background: #f5f7fa;
        border-radius: 8px;
        padding: 0 16px;
        height: 56px;
        margin-bottom: 8px;
        
        .country-code-selector {
          display: flex;
          align-items: center;
          gap: 4px;
          padding-right: 12px;
          cursor: pointer;
          
          .country-code {
            font-size: 16px;
            color: #303133;
            font-weight: 500;
          }
          
          .arrow-down {
            font-size: 12px;
            color: #909399;
          }
        }
        
        .input-divider {
          width: 1px;
          height: 24px;
          background: #dcdfe6;
          margin-right: 12px;
        }
        
        .phone-input {
          flex: 1;
          border: none;
          background: transparent;
          font-size: 16px;
          color: #303133;
          outline: none;
          
          &::placeholder {
            color: #c0c4cc;
          }
        }
      }
      
      .error-message {
        font-size: 12px;
        color: #f56c6c;
        margin-top: 8px;
        text-align: left;
        padding: 0 4px;
      }
    }
    
    .dialog-footer {
      padding: 16px 20px 24px 20px;
      border-top: 1px solid #f0f0f0;
      
      .confirm-button {
        width: 100%;
        height: 48px;
        border: none;
        border-radius: 8px;
        background: #ff6b9d;
        color: #ffffff;
        font-size: 16px;
        font-weight: 500;
        cursor: pointer;
        transition: all 0.2s;
        
        &:hover:not(.disabled) {
          background: #ff5a8a;
        }
        
        &:active:not(.disabled) {
          background: #ff4a7a;
        }
        
        &.disabled {
          background: #e4e7ed;
          color: #c0c4cc;
          cursor: not-allowed;
        }
      }
    }
  }
}
</style>

